package com.itheima.admin.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.admin.Service.AdSensitivesService;
import com.itheima.admin.mapper.AdSensitivesMapper;
import com.itheima.model.common.admin.dtos.SensitivesDto;
import com.itheima.model.common.admin.pojos.AdSensitives;
import com.itheima.model.common.dtos.PageResponseResult;
import com.itheima.model.common.dtos.ResponseResult;
import com.itheima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdSensitivesServiceImpl extends ServiceImpl<AdSensitivesMapper,AdSensitives> implements AdSensitivesService {


    @Override
    public ResponseResult findbyNameAndPage(SensitivesDto sensitivesDto) {
        if (sensitivesDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        sensitivesDto.checkParam();
        Page page1 = new Page(sensitivesDto.getPage(), sensitivesDto.getSize());
        LambdaQueryWrapper<AdSensitives> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //判读是否为空
        if (StringUtils.isNoneBlank(sensitivesDto.getName())){
            lambdaQueryWrapper.like(AdSensitives::getSensitives,sensitivesDto.getName());

        }
        IPage result = page(page1,lambdaQueryWrapper);
        ResponseResult responseResult = new PageResponseResult(sensitivesDto.getPage(),sensitivesDto.getSize(),(int)result.getTotal());
        responseResult.setData(result.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult insert(AdSensitives adSensitives) {
        if (adSensitives==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //新增id必须为空
        //敏感词不能为空
        if (!StringUtils.isNoneBlank(adSensitives.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.NOTEMPYT);
        }
        //新增id必须为空

        if (adSensitives.getId()!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.MUSTEMPYT);
        }
        //敏感词不能为空,长度限制

        if(adSensitives.getSensitives().length()>10){
            return ResponseResult.errorResult(AppHttpCodeEnum.TOO_LONG);
        }
        //判断是否库中存在
        List<AdSensitives> list = list();
        for (AdSensitives sensitives : list) {
            if(sensitives.getSensitives().equals(adSensitives.getSensitives())){
                return ResponseResult.errorResult(AppHttpCodeEnum.NAME_SAME);
            }
        }

        adSensitives.setCreatedTime(new Date());
        save(adSensitives);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

    @Override
    public ResponseResult update(AdSensitives adSensitives) {
        if(adSensitives.getSensitives().length()>10){
            return ResponseResult.errorResult(AppHttpCodeEnum.TOO_LONG);
        }
        if (adSensitives==null||adSensitives.getId()==null){
            return ResponseResult.errorResult((AppHttpCodeEnum.PARAM_INVALID));
        }

        //adChannel.setCreatedTime(new Date());
        updateById(adSensitives);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
//id不为空 判断id有效范围
    @Override
    public ResponseResult deleteById(Integer id) {
        if (id==null){
            return ResponseResult.errorResult((AppHttpCodeEnum.PARAM_INVALID));
        }
        AdSensitives sensitives = getById(id);
        if (sensitives==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

       // int i=3/0;
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
